package com.zhaolin.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.base.Splitter;
import com.zhaolin.entity.AnswerOpt;
import com.zhaolin.entity.AnswerTxt;
import com.zhaolin.entity.Survey;
import com.zhaolin.mapper.AnswerOptDao;
import com.zhaolin.mapper.AnswerTxtDao;
import com.zhaolin.mapper.QuestionOptDao;
import com.zhaolin.mapper.SurveyDao;
import com.zhaolin.service.SurveyService;
import com.zhaolin.utils.BeanMapUtils;
import com.zhaolin.utils.MapControl;
import com.zhaolin.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SurveyServiceImpl implements SurveyService {

    @Autowired
    SurveyDao surveyDao;

    @Autowired
    AnswerOptDao answerOptDao;

    @Autowired
    AnswerTxtDao answerTxtDao;

    @Autowired
    QuestionOptDao questionOptDao;

    @Override
    public int create(Survey pi) {
        return surveyDao.create(pi);
    }

    @Override
    public int delete(String ids) {
        int flag = 0;
        List<String> list = Splitter.on(",").splitToList(ids);
        for (String s : list) {
            StringBuffer stringBuffer = new StringBuffer(s);
            StringBuffer delete = stringBuffer.delete(0,4);
            surveyDao.delete(MapParameter.getInstance().add("id",Integer.parseInt(delete.toString())).getMap());
            flag++;
        }
        return flag;
    }

    @Override
    public int update(Survey pi) {
        Map<String,Object>map =MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(pi)).add("id", pi.getId()).getMap();
        return surveyDao.update(map);
    }

    @Override
    public List<Survey> query(Survey pi) {
        PageHelper.startPage(pi.getPage(), pi.getLimit());
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(pi)).getMap();
        return surveyDao.query(map);
    }

    @Override
    public List<Survey> queryAll(Survey pi){
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(pi)).getMap();
        return surveyDao.query(map);
    }

    @Override
    public Integer submit(List<AnswerOpt> listOpt, List<AnswerTxt> listTxt) {
        int flag = 0;
        for (AnswerOpt num:listOpt){
            flag = answerOptDao.create(num);
        }
        for (AnswerTxt num:listTxt){
            flag = answerTxtDao.create(num);
        }
        return flag;
    }

    @Override
    public List<AnswerOpt> querydetail(AnswerOpt questionOpt){
        Map<String, Object> map = MapControl.getInstance().put(BeanMapUtils.beanToMap(questionOpt)).getMap();
        List<AnswerOpt> query = answerOptDao.query(map);
        return query;
    }


    @Override
    public Survey detail(Integer id) {
        return surveyDao.detail(MapControl.getInstance().add("id",id).getMap());
    }


    @Override
    public int count(Survey pi) {
        return 0;
    }

    @Override
    public List<Integer> queryByState(String state) {
        return null;
    }

    @Override
    public List<Integer> queryByStateForExec(String state) {
        return null;
    }

    @Override
    public void updateState() {
        //先查询出来然后更新他的状态
        List<Integer> integers = surveyDao.queryByState(Survey.state_create);
        for (Integer id : integers){
            surveyDao.update(MapParameter.getInstance().add("updateState",Survey.state_exec).add("id",id).getMap());
        }
        List<Integer> integers2 = surveyDao.queryByStateForExec(Survey.state_exec);
        for (Integer id : integers2){
            System.out.println("结束信息");
            surveyDao.update(MapParameter.getInstance().add("updateState",Survey.state_over).add("id",id).getMap());
        }
    }
    }

